/*********************
 * @author hetao
 * 
 * **********************************************************
 * 
 */
 var REDIRECTURL = $.cookie('_LAST_URL_'); // 读取 cookie
 
 function redirectUrl(){
	 location.href = REDIRECTURL.replace(/\"/g, ""); 
 }
/**
 * 判断当前是否处在iframe中
 * 
 * @method isIframe
 * @return {Boolean}
 */
window.isIframe = function() {
	return top.location != self.location;
}

/**
 * 判断当前不处在iframe中
 * 
 * @method isIframe
 * @return {Boolean}
 */
window.isNotIframe = function() {
	return !isIframe();
};
 


$(function(){
	
	$(".i-checks").iCheck({
		checkboxClass : "icheckbox_square-green",
		radioClass : "iradio_square-green",
	})
	
	
	
					/* ---------- List ---------- */
					var $listForm = $("#listForm");// 列表表单
					var $listTable = $("#listTable");// 列表表单
					var $listTableTr = $("#listTable tr:gt(0)");
					var $searchButton = $("#searchButton");// 查找按钮
					var $allCheck = $("#listTable input[name='btSelectAll']");// 全选复选框
					var $idsCheck = $("#listTable input[name='btSelectItem']");//ID复选框
					var $addButton = $("#addButton"); // 新增
					var $editButton = $("#editButton"); // 编辑
					var $importButton = $("#importButton"); // 导入
					var $exportButton = $("#exportButton"); // 导出
					var $removeButton = $("#removeButton");// 删除按钮
					
					var $validateForm = $("#validateForm");
					var $validateForm = $("#validateForm");
					
					//全局表单验证方法
					var submit = function (){
					}
					
					if ($validateForm.size() > 0) {
						$.metadata.setType("attr", "validate");
						$validateForm.validate({
							errorClass: "help-block m-b-none",
					    	validClass: "help-block m-b-none",
					    	 highlight: function(e) {
					    		$(e).closest(".form-group").removeClass("has-success").addClass("has-error");
					    	}, 
					    	success: function(e) {
					    		e.closest(".form-group").removeClass("has-error").addClass("has-success");
					    	},
					    	errorElement: "span",
					    	errorPlacement: function(e, r) {
					    		e.appendTo(r.is(":radio") || r.is(":checkbox") ? r.parent().parent().parent() : r.parent());
					    	},
							ignoreTitle: true,
							ignore: ".ignoreValidate",
							submitHandler: function(form) {
								$(form).find(":submit").attr("disabled", true);
								//form.submit();
								var param = $validateForm.serialize();
							     $.ajax({
										url : $validateForm.attr("action"),
										type : "post",
										dataType : "json",
										data : param,
										success : function(result) {
											if (result.success) {
												parent.toastrs.success("操作成功");
												parent.layers.close();
											 } else {
												//var jsonObj = eval('(' + result + ')');
												// parent.layers.msg("操作失败");
												 parent.toastrs.error("操作失败");
											}
										}
									});
									
							}
						});
					}
				/*	if ($listForm.size() > 0) {
						if (!!!$listForm.attr("action")) {
							$listForm.attr("action", "operate.action");
						}
						$listForm.attr("method", "get");
					}*/
						// 新增
						$addButton.click(function() {
							var url = $(this).attr("url");
							if(isNotIframe()){
								  layers.open(url, $(this).attr("text"),redirectUrl);
								}else{
									parent.layers.open(url, $(this).attr("text"),redirectUrl);
								}			
					
						});
						
					    
						// 编辑
						$editButton.click(function() {
							        var $this = $(this);
									var url = $(this).attr("url");
									 var $idsCheckedCheck = $("#listTable input[name='btSelectItem']:checked");
									if ($idsCheckedCheck.size() > 1) {
										//parent.layers.msg('sorry！只能操作一条数据'); 
										parent.toastrs.warning("sorry！只能操作一条数据");
										return false;
									}
									if($idsCheckedCheck.size() == 0){
										return false;
									}
									if(isNotIframe()){
									  layers.open(url + "?id=" + $idsCheckedCheck.val(), $this.attr("text"),redirectUrl);
									}else{
										parent.layers.open(url + "?id=" + $idsCheckedCheck.val(), $this.attr("text"),redirectUrl);
									}
                                  
								});
						// 批量删除
						$removeButton.click(function() {
									var url = $(this).attr("url");
									 var $idsCheckedCheck = $("#listTable input[name='btSelectItem']:checked");
									if ($idsCheckedCheck.length == 0) {
										return;
									}
									var isAllowDel = true;
									/*$idsCheckedCheck.each(function() {
												if ($(this).attr("isAllowDel") && $(this).attr( "isAllowDel") == 0) {
													isAllowDel = false;
													return;
												}
											});*/
									if (!isAllowDel) {
										 //无权限
									}else {
										parent.layers.confirm(function(){
											var param = $idsCheckedCheck.serialize();
											var ps = [];
											$.each($idsCheckedCheck,function(i,n){
								        		ps.push($(n).val());
								        	});
											$.ajax({
												url : url,
												type : "post",
												dataType : "json",
												data : {"ids":ps.join(",")},
												success : function(result) {
													if (result.success) {
														parent.toastrs.success("操作成功");
														redirectUrl();
													 } else {
														//var jsonObj = eval('(' + result + ')');
														// parent.layers.msg("操作失败");
														 parent.toastrs.error("操作失败");
													}
												}
											});
										});
									}
								});
						
						// 导入
						$importButton.click(function() {
							var url = $(this).attr("url");
							location.href = url;
//							if (preImport && $.isFunction(preImport)) {
//								preImport();
//							}
						});
//
//						// 导出
//						$exportButton.click(function() {
//							var url = $(this).attr("url");
//							if (aftExport && $.isFunction(aftExport)) {
//								aftExport();
//							}
//						});

						 

						// 审核
						/*$auditButton
								.click(function() {
									var $idsCheckedCheck = $("#listTable input[name='ids']:checked");
									var isError = true;
									 
									if ($idsCheckedCheck.size() != 1) {
										$.dialog({
											type : "warn",
											content : "只能选择一条记录",
											cancel : "取 消",
											modal : true
										});
										return;
									}
									if ($idsCheckedCheck.attr("isaudit") == -1) {
										$.dialog({
											type : "warn",
											content : "该商品已经被审核拒绝",
											cancel : "取 消",
											modal : true
										});
										isError = false;
									} else if ($idsCheckedCheck.attr("isaudit") == 1) {
										$.dialog({
											type : "warn",
											content : "该商品已经被审核",
											cancel : "取 消",
											modal : true
										});
										isError = false;
									}

									if (!isError) {
										return;
									}
									 
									if ($idsCheckedCheck.size() <= 0)
										return;
									if (saveAudit && $.isFunction(saveAudit)) {
										saveAudit($(this).attr("url")+"&"+$idsCheckedCheck.serialize());
									}
								});

						// 申请审核
						$auditapplyButton
								.click(function() {
									var $idsCheckedCheck = $("#listTable input[name='ids']:checked");
									var isError = true;
									$idsCheckedCheck.each(function() {
										if ($(this).attr("isaudit") == 1) {
											$.dialog({
												type : "warn",
												content : "选择有误,存在已经被审核通过的商品",
												cancel : "取 消",
												modal : true
											});
											isError = false;
										} else if ($idsCheckedCheck.attr("isaudit") == 0) {
											$.dialog({
												type : "warn",
												content : "选择有误,存在审核中的商品",
												cancel : "取 消",
												modal : true
											});
											isError = false;
										}
									});
									if (!isError) {
										return;
									}
									if ($idsCheckedCheck.size() <= 0)
										return;
									location.href = $(this).attr("url")+"&"+$idsCheckedCheck.serialize();
								});

						          
						
						// 无复选框被选中时,删除按钮不可用
						$idsCheck
								.click(function(event) {
									var $this = $(this);
									if ($this.attr("checked")) {
										$this.parent().parent().addClass(
												"checked");
										$removeButton.attr("disabled", false);
										$editButton.attr("disabled",false);
									} else {
										$this.parent().parent().removeClass(
												"checked");
										var $idsChecked = $("#listTable input[name='ids']:checked");
										if ($idsChecked.size() > 0) {
											$removeButton.attr("disabled",
													false);
											$editButton.attr("disabled",false);
										} else {
											$removeButton
													.attr("disabled", true);
											$editButton.attr("disabled",true);
										}
									}
									event.stopPropagation();
								});
*/
						

						// 查找
						$searchButton.click(function() {
							$("#pageNumber").val("1");
							$listForm.submit();
						});

					 
					  

					});
